package cn.itcast.bos.service.system.Impl;

import cn.itcast.bos.Dao.system.RoleRespostiory;
import cn.itcast.bos.Dao.system.UserRestiory;
import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRestiory userRestiory;
    @Autowired
    private RoleRespostiory roleRespostiory;


    @Override
    public User findByUsername(String username) {
        return userRestiory.findByUsername(username);
    }

    @Override
    public List<User> findAll() {
        return userRestiory.findAll();
    }

    @Override
    public void save(User user, String[] roleIds) {
        userRestiory.save(user);
        //关联保存角色
        if(roleIds.length !=0 && roleIds !=null){
            for(String id : roleIds){
                Role role = roleRespostiory.findOne(Integer.parseInt(id));
                user.getRoles().add(role);
            }

        }
    }
}
